//
//  RAMainViewController.m
//  RAFSUManagement
//
//  Created by Piccolo on 21/03/2018.
//  Copyright © 2018 Rayonics. All rights reserved.
//

#import "RAMainViewController.h"
#import "RAProductViewController.h"
#import "RAFSUTestViewController.h"
#import "ZJUsefulPickerView.h"

@interface RAMainViewController () {
    
    __weak IBOutlet UILabel *_operatorTypeLabel;
    __weak IBOutlet UILabel *_manufacturerLabel;
    __weak IBOutlet UILabel *_versionLabel;
    BOOL _isFirstSetting;
    NSRAOperatorType _operatorType;
    NSString *_manufacturer;
}
- (IBAction)productBtnOnClick:(id)sender;
- (IBAction)testBtnOnClick:(id)sender;
- (IBAction)qualityBtnOnClick:(id)sender;

@end

@implementation RAMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupVersionLabel];
    [self setupInitialization];
    [self addGestureRecognizerToLabels];
    [self showOperatorPickerView];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self setupNavigationBar];
}

#pragma mark - utils

- (void)setupVersionLabel {
    
    _versionLabel.text = APP_VERSION;
}

- (void)setupInitialization {
    
    _isFirstSetting = YES;
}

- (void)setupNavigationBar {
    
    self.navigationController.navigationBarHidden = YES;
}

- (void)addGestureRecognizerToLabels {
    
    UITapGestureRecognizer *operatorLabelGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showOperatorPickerView)];
    // if labelView is not set userInteractionEnabled, you must do so
    [_operatorTypeLabel setUserInteractionEnabled:YES];
    [_operatorTypeLabel addGestureRecognizer:operatorLabelGesture];
    
    UITapGestureRecognizer *manufacturerLabelGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showManufacturerPickerView)];
    // if labelView is not set userInteractionEnabled, you must do so
    [_manufacturerLabel setUserInteractionEnabled:YES];
    [_manufacturerLabel addGestureRecognizer:manufacturerLabelGesture];
}

- (void)showOperatorPickerView {
    
    [ZJUsefulPickerView showSingleColPickerWithToolBarText:@"运营商选择" withData:RA_OPERATOR_ARRAY withDefaultIndex:_operatorType ? _operatorType - 1 : NSRAOperatorTypeCNTT - 1 withCancelHandler:^{
        NSLog(@"@652quxiaole -----");
        if (![RAUtil checkStringExist:_operatorTypeLabel.text]) {
            [RAUtil showAlertWithMessage:@"必须选择运营商" alertType:NSRAAlertTypeError];
            [self showOperatorPickerView];
        }
        
    } withDoneHandler:^(NSInteger selectedIndex, NSString *selectedValue) {
        NSLog(@"%@---%ld", selectedValue, selectedIndex);
        _operatorType = (NSRAOperatorType)(selectedIndex + 1);
        [self updateOperator];
        if (![RAUtil checkStringExist:_manufacturerLabel.text]) {
            [self showManufacturerPickerView];
        }
    }];
}

- (void)showManufacturerPickerView {
    
    [ZJUsefulPickerView showSingleColPickerWithToolBarText:@"厂商选择" withData:RA_MANUFACTURER_ARRAY withDefaultIndex:0 withCancelHandler:^{
        NSLog(@"@652quxiaole -----");
        if (![RAUtil checkStringExist:_manufacturerLabel.text]) {
            [RAUtil showAlertWithMessage:@"必须选择厂商" alertType:NSRAAlertTypeError];
            [self showManufacturerPickerView];
        }
        
    } withDoneHandler:^(NSInteger selectedIndex, NSString *selectedValue) {
        NSLog(@"%@---%ld", selectedValue, selectedIndex);
        _manufacturer = selectedValue;
        [self updateManufacturer];
    }];
}

- (void)updateOperator {
    
    _operatorTypeLabel.text = [NSString stringWithFormat:@"运营商:%@", [RAUtil stringFromOperatorType:_operatorType]];
}

- (void)updateManufacturer {
    
    _manufacturerLabel.text = [NSString stringWithFormat:@"厂商:%@", _manufacturer];
}

- (BOOL)checkInitializationSelect {
    
    if (!_operatorType) {
        [self showOperatorPickerView];
        return NO;
    }
    if (![RAUtil checkStringExist:_manufacturer]) {
        [self showManufacturerPickerView];
        return NO;
    }
    return YES;
}

#pragma mark - button action

- (IBAction)productBtnOnClick:(id)sender {
    
    if (![self checkInitializationSelect]) {
        return;
    }
    RAProductViewController *productViewController = [[RAProductViewController alloc] initWithNibName:@"RAProductViewController" bundle:nil];
    productViewController.operatorType = _operatorType;
    [RAUtil pushViewController:self toTargetViewController:productViewController animated:YES];
}

- (IBAction)testBtnOnClick:(id)sender {
    
    if (![self checkInitializationSelect]) {
        return;
    }
    RAFSUTestViewController *fsuTestViewController = [[RAFSUTestViewController alloc] initWithNibName:@"RAFSUTestViewController" bundle:nil];
    fsuTestViewController.operatorType = _operatorType;
    [RAUtil pushViewController:self toTargetViewController:fsuTestViewController animated:YES];
}

- (IBAction)qualityBtnOnClick:(id)sender {
    
    if (![self checkInitializationSelect]) {
        return;
    }
    if (![self checkInitializationSelect]) {
        return;
    }
    RAFSUTestViewController *fsuTestViewController = [[RAFSUTestViewController alloc] initWithNibName:@"RAFSUTestViewController" bundle:nil];
    fsuTestViewController.operatorType = _operatorType;
    [RAUtil pushViewController:self toTargetViewController:fsuTestViewController animated:YES];
}
@end
